<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title></title>
		<link rel="stylesheet" type="text/css" href="css/base.css" />
		<link rel="stylesheet" type="text/css" href="css/my_reg.css" />
		<script src="js/jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
		<script src="//cdn1.lncld.net/static/js/2.1.3/av-min.js"></script>
		<style>
			#selecter{

				width: 100%;
				height: 30px;
				margin-left: -33px;
				background-color: red;
				color: white;
				padding-left: 100px;
				margin-top: 5px;
			}
			#selecter>option{
				height: 30px;
			}
			.t2 option{
				text-align: center;
				
			}
			#my_zhuce .zhuce_div span {
				font-size: 12px;
				text-align: center;
				margin: 0px auto;
			}
			#sp4{
				display: none;
			}
		</style>
	</head>

	<body>
		<div id="wu_quan">
			<div id="header">
				<!-- logo -->
				<a href="#" id="logo"><img src="img/logo.png"></a>
				<div id="navmenu">
					<nav id="menu">
						<ul>
							<li >
								<a href="index.html">首页</a>
							</li>
							<li >
								<a href="productlist.html">买车</a>
							</li>
							<li >
								<a href="">卖车</a>
							</li>
							<li>
								<a href="xinche.html">新车</a>
							</li>
							<li >
								<a href="baodian.html">宝典</a>
							</li>
							<li>
								<a href="fwbz.html">服务保障</a>
							</li>
							<li class="active">
								<a href="my_reg.html">注册</a>
							</li>
							<li>
								<a href="my_register.html">登录</a>
							</li>
						</ul>
					</nav>
					<div id="search">
						<form role="search" method="get">
							<input type="text" placeholder="search..." name="s" value="" autocomplete="off" />
						</form>
					</div>
				</div>
			</div>
			<br><br><br><br><br><br><br><br><br>
			<br><br>
			<div style="text-align:center;clear:both">
			</div>
		</div>
		<div id="wu_zong">

			<div id="my_zhuce">
				<div class="zhuce_right">
					<div class="zhuce_div">
						<form>
							<table border="0" cellspacing="0" cellpadding="0">
								<tr class="t1">
									<td id="zc"> 注册</td>
								</tr>
								<tr>
									<td colspan="2"><input type="text" id="username" value="" placeholder="用户名" /></td>
								</tr>
								<tr>
									<td colspan="2"><span id="sp1">请输入用户名,建议用真实姓名</span><span id="sp4">该用户已被注册</span></td>
								</tr>
								<tr>
									<td colspan="2"><input type="text" id="phone" value="" placeholder="请输入电话号码" /></td>
									<!--<td><input type="button" id="huoqu" value="获取动态密码" /></td>-->
								</tr>
								<tr>
									<td colspan="2"><span id="sp2">请填写正确电话号码</span></td>
								</tr>
								<tr>
									<td colspan="2"><input type="password" id="pwd" value="" placeholder="请输入密码" /></td>
								</tr>
								<tr>
									<td colspan="2"><span id="sp3">请设置登录密码</span></td>
								</tr>
								<tr>
									<td colspan="2"><input type="button" id="zhuce" value="立即注册" /></td>
								</tr>
								<tr class="t2">
									<!--<td colspan="1"><input type="checkbox" id="denglu" />自动登录</td>-->
									<td colspan="2">
										<select id="selecter" onchange="getRoles()">
											<option value="buyer">买家</option>
											<option value="seler">卖家</option>
										</select>
									</td>
								</tr>
							</table>
						</form>
					</div>
				</div>
			</div>
		</div>
		<div id="wu_foot">
			<div id="wu_foot_foot">
				<ul>
					<li>
						<a href="#">关于我们</a>
					</li>
					<li>
						<a href="#">加入我们</a>
					</li>
					<li>
						<a href="#">帮助中心</a>
					</li>
					<li>
						<a href="#">用户反馈</a>
					</li>
					<li>
						<a href="#">商家申请</a>
					</li>
					<li>
						<a href="#">鹏鹏易购</a>
					</li>
					<li>
						<a href="#">网站地图</a>
					</li>
					<div class="fr m10">
						<img src="img/code.jpg" />
						<p class="f16">微信公众号</p>
						<p class="f13">微信动态早知道</p>
					</div>
					<div class="fr m10">
						<img src="img/code.jpg" />
						<p class="f16">APP下载</p>
						<p class="f13">车源更新不错过</p>
					</div>
				</ul>
			</div>
		</div>
	</body>
	<script type="text/javascript">
	
	var username = $("#username");
		var reg =  /^[\u4e00-\u9fff\w]{5,16}$/;
		var t1 = false;
		var t2 = false;
		var t3 = false;
		username.blur(function() {
			$("#sp1").html(username.val() == "" ? "<img align='center' src='img/ma_img/onError.gif' />用户名不能为空" : (reg.test(username.val()) == false ? "<img align='center' src='img/ma_img/onError.gif' />至少5个字符 ": "<img align='center' src='img/ma_img/onsuccess.gif' />该用户名可以使用"));
			username.val() == "" ? t1 = false : (reg.test(username.val()) == false ? t1 = false : t1 = true);
		});
		var phone = $("#phone");
		var phones = /^1[3|4|5|7|8][0-9]{9}$/;
		phone.blur(function() {
			$("#sp2").html(phone.val() == "" ? "<img align='center' src='img/ma_img/onError.gif' />手机号不能为空！" : (phones.test(phone.val()) == false ? "<img align='center' src='img/ma_img/onError.gif' />手机格式不符合！" : "<img align='center' src='img/ma_img/onsuccess.gif' />该手机可以使用"));
			phone.val() == "" ? t2 = false : (phones.test(phones.val()) == false ? t2 = false : t2 = true);
		});
		var pwd = $("#pwd");
			var rew =/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;
			pwd.blur(function() {
				$("#sp3").html(pwd.val() == "" ? "<img align='center' src='img/ma_img/onError.gif' />请输入6-12位数字和字母组成的密码！" : (rew.test(pwd.val()) == false ? "<img align='center' src='img/ma_img/onError.gif' />密码格式不符合！" : "<img align='center' src='img/ma_img/onsuccess.gif' />该密码可以使用"));
				pwd.val() == "" ? t2 = false : (rew.test(pwd.val()) == false ? t2 = false : t2 = true);
				//				console.log(t2)
			});

		function zhuce() {
			var username = $('#username').val();
			var password = $('#pwd').val();
			var phone = $('#phone').val();
			var user = new AV.User();
			if(t1 == true && t2 == true && t3 == true ) {
				user.setUsername(username);
				user.setPassword(password);
				user.setEmail(email);
				user.signUp().then(function(loginedUser) {
					alert("注册成功")
						// 注册成功，跳转到商品 list 页面
				}, (function(error) {
					//				alert(JSON.stringify(error));
					console.log(error)
					alert("请重新注册1")
				}));
			} else {
				alert("请重新注册")
			}

		}
		var roleId = "58caa22c44d90400682bae0b"; //默认为买家
		function getRoles() {
			var rolesIndex = $("#selecter").get(0).selectedIndex;
			if(rolesIndex == 0) {
				roleId = "58caa22c44d90400682bae0b";
			} else if(rolesIndex == 1) {
				roleId = "58caa211128fe1006c80392f";
			}
		}
		//		错误码JSON文件解析
		var errocode;
		$(function() {
			$.getJSON("json/errocode.json", function(data) {
				errocode = data;
			});
		});
		//注册
		var username;
		$("#zhuce").click(function() {
			$.ajax({
				type: "post",
				url: " https://api.leancloud.cn/1.1/users",
				async: true,
				data: '{"username":"' + $("#username").val() + '","password":"' + $("#pwd").val() + '","phone":"' + $("#phone").val() + '"}',
				headers: {
					"X-LC-Id": "9keh2YX5j8hpDc0YR2REhv0q-gzGzoHsz",
					"X-LC-Key": "rrdO8OEJACH5qna3m3FzhpYf",
					"Content-Type": "application/json"
				},
				success: function(jsondata) {
					console.log(jsondata);
					username = jsondata.username;
					var objectSessionToken = jsondata.sessionToken;
					var objectid = jsondata.objectId;
					addRelations(roleId,objectid,objectSessionToken)
				},
				error: function(e) {
					var code = JSON.parse(e.responseText).code;
					for(var i in errocode) {
						if(errocode[i].code == code) {
							alert(errocode[i].content);
							console.log(errocode[i].content)
						}
					}
				}
			});
		});
		//在刚刚注册的用户中，创建一个关联，关联到某一个角色（卖家/买家）

		//创建所属角色put请求的data数据
		function secOutKey(roleId) {
			var sessionObject = new Object;
			sessionObject.Roler = {
				"__type": "Pointer",
				"className": "_Role",
				"objectId": roleId
			}
			var jsons = JSON.stringify(sessionObject);
			return jsons;
		}
		//创建关系
		function addRelations(roleId, userId,sessionToken) {
			console.log(secOutKey(userId));
			$.ajax({
				type: "put",
				url: "https://api.leancloud.cn/1.1/users/" + userId,
				async: true,
				headers: {
					"X-LC-Id": "9keh2YX5j8hpDc0YR2REhv0q-gzGzoHsz",
					"X-LC-Key": "rrdO8OEJACH5qna3m3FzhpYf",
					"X-LC-Session": sessionToken,
					"Content-Type": "application/json"
				},
				data: secOutKey(roleId),
				success: function(data) {
//					console.log(data);
					location.href="my_register.html?"+username
				},
				error: function(e) {
					var code = JSON.parse(e.responseText).code;
					console.log(code);
					for(var i in errocode) {
						if(errocode[i].code == code) {
							console.log(errocode[i].content)
						}
					}
				}
			});
		}
	</script>

</html>